package com.bwie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.pojo.TbCoupon;
import com.bwie.service.TbCouponService;
import com.bwie.mapper.TbCouponMapper;
import com.bwie.utils.R;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
* @author Yoo19
* @description 针对表【tb_coupon】的数据库操作Service实现
* @createDate 2025-07-15 11:52:31
*/
@Service
public class TbCouponServiceImpl extends ServiceImpl<TbCouponMapper, TbCoupon>
    implements TbCouponService{
    @Autowired
    private TbCouponMapper tbCouponMapper;
    @Autowired
    private HttpServletRequest request;
    @Override
    public R getCouponList() {
        List<TbCoupon> tbCoupons = tbCouponMapper.selectList(null);
        return R.OK(tbCoupons);
    }

    @Override
    public R getUserCoupon() {
        String token = request.getHeader("token");
        Claims claims = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody();
        Integer userId = claims.get("userId", Integer.class);

       List<TbCoupon> tbCouponList =  tbCouponMapper.getUserCoupon(userId);
        return R.OK(tbCouponList);
    }

    @Override
    public R addCoupon(TbCoupon tbCoupon) {
        tbCouponMapper.insert(tbCoupon);
        return R.OK();
    }

    @Override
    public R updateCoupon(TbCoupon tbCoupon) {
        tbCouponMapper.updateById(tbCoupon);
        return R.OK();
    }

    @Override
    public R deleteCoupon(Integer couponId) {
        tbCouponMapper.deleteById(couponId);
        return R.OK();
    }
}




